home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / bildschirmschoner / garshneblanker / gsource / blankers / clock / blank.c next >
C/C++ Source or Header  |  1996-04-07  |  5KB  |  254 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <utility/date.h>
  11. #include <hardware/custom.h>
  12.  
  13. #include <clib/utility_protos.h>
  14. #include <pragmas/utility_pragmas.h>
  15. #include <clib/diskfont_protos.h>
  16. #include <pragmas/diskfont_pragmas.h>
  17.  
  18. #include <string.h>
  19.  
  20. #include "/includes.h"
  21.  
  22. extern __far struct Custom custom;
  23.  
  24. #define MIL   0
  25. #define SECS  1
  26. #define FONT  3
  27. #define CYCLE 5
  28. #define DELAY 6
  29. #define MODE  8
  30.  
  31. LONG Hour[] = {12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11};
  32.  
  33. struct Library *UtilityBase;
  34. struct Library *DiskfontBase;
  35.  
  36. VOID Defaults( PrefObject *Prefs )
  37. {
  38.     Prefs[MIL].po_Active = 1;
  39.     Prefs[SECS].po_Active = 1;
  40.     Prefs[CYCLE].po_Active = 0;
  41.     strcpy( Prefs[FONT].po_Name, "topaz.font" );
  42.     Prefs[FONT].po_Attr.ta_YSize = 11;
  43.     Prefs[DELAY].po_Level = 60;
  44.     Prefs[MODE].po_ModeID = getTopScreenMode();
  45. }
  46.  
  47. LONG getTime( BYTE *time, PrefObject *Prefs )
  48. {
  49.     struct DateStamp Stamp;
  50.     struct ClockData Data;
  51.     LONG pos = 0, hour;
  52.     
  53.     DateStamp( &Stamp );
  54.     Amiga2Date((( Stamp.ds_Days * 1440 ) + Stamp.ds_Minute ) * 60 +
  55.                Stamp.ds_Tick/TICKS_PER_SECOND, &Data );
  56.     
  57.     if( !Prefs[MIL].po_Active )
  58.         hour = Hour[Data.hour];
  59.     else
  60.         hour = Data.hour;
  61.  
  62.     if( hour > 9 )
  63.         time[pos++] = ( BYTE )( hour / 10 ) + '0';
  64.     time[pos++] = ( BYTE )( hour % 10 ) + '0';
  65.  
  66.     time[pos++] = ':';
  67.  
  68.     time[pos++] = ( BYTE )( Data.min / 10 ) + '0';
  69.     time[pos++] = ( BYTE )( Data.min % 10 ) + '0';
  70.  
  71.     if( Prefs[SECS].po_Active )
  72.     {
  73.         time[pos++] = ':';
  74.         time[pos++] = ( BYTE )( Data.sec / 10 ) + '0';
  75.         time[pos++] = ( BYTE )( Data.sec % 10 ) + '0';
  76.     }
  77.  
  78.     if( !Prefs[MIL].po_Active )
  79.     {
  80.         if( Data.hour > 11 )
  81.             time[pos++] = 'p';
  82.         else
  83.             time[pos++] = 'a';
  84.         time[pos++] = 'm';
  85.     }
  86.  
  87.     time[pos] = '\0';
  88.     
  89.     return ( LONG )strlen( time );
  90. }
  91.  
  92. LONG Diff( BYTE *One, BYTE *Two )
  93. {
  94.     while( *One && *Two )
  95.         if( *One++ != *Two++ )
  96.             return TRUE;
  97.  
  98.     return *One != *Two;
  99. }
  100.  
  101. LONG Blank( PrefObject *Prefs )
  102. {
  103.     struct TextFont *font;
  104.     struct Screen *Scr;
  105.     struct Window *Wnd;
  106.     struct RastPort *RP;
  107.     struct ViewPort *VP;
  108.     LONG vals[] = {
  109.         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10,
  110.         9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  111.     BYTE scrClock[32], oldClock[32];
  112.     LONG ScrToFrontCnt=0, c1 = 0, c2 = 14, c3 = 28, delay, fonty, RetVal = OK;
  113.     LONG x, y, Wid, Hei, len, nlen, numc, base, count = 0, dx = -1, dy = -1;
  114.     
  115.     DiskfontBase = OpenLibrary( "diskfont.library", 37 );
  116.     UtilityBase = OpenLibrary( "utility.library", 37 );
  117.  
  118.     if( !DiskfontBase || !UtilityBase )
  119.         goto JAIL;
  120.  
  121.     delay = Prefs[DELAY].po_Level;
  122.     
  123.     Prefs[FONT].po_Attr.ta_Name = Prefs[FONT].po_Name;
  124.     font = OpenDiskFont(&( Prefs[FONT].po_Attr ));
  125.     if( !font )
  126.     {
  127.         strcpy( Prefs[FONT].po_Attr.ta_Name, "topaz.font" );
  128.         font = OpenDiskFont(&( Prefs[FONT].po_Attr ));
  129.     }
  130.     base = font->tf_Baseline;
  131.     fonty = Prefs[FONT].po_Attr.ta_YSize;
  132.     
  133.     Scr = OpenScreenTags( 0l, SA_DisplayID, Prefs[MODE].po_ModeID, SA_Depth, 1,
  134.                          SA_Quiet, TRUE, SA_Overscan, OSCAN_STANDARD,
  135.                          SA_Behind, TRUE, SA_Font, &Prefs[FONT].po_Attr,
  136.                          TAG_DONE );
  137.     if( Scr )
  138.     {
  139.         Wid = Scr->Width;
  140.         Hei = Scr->Height;
  141.         RP = &( Scr->RastPort );
  142.         VP = &( Scr->ViewPort );
  143.         
  144.         SetRGB4( VP, 0, 0L, 0L, 0L );
  145.         switch( Prefs[CYCLE].po_Active )
  146.         {
  147.         case 0:
  148.             SetRGB4( VP, 1, vals[c1], vals[c2], vals[c3] );
  149.             break;
  150.         case 1:
  151.             SetRGB4( VP, 1, ( ULONG )RangeRand( 15 ) + 1,
  152.                     ( ULONG )RangeRand( 15 ) + 1,
  153.                     ( ULONG )RangeRand( 15 ) + 1 );
  154.             break;
  155.         case 3:
  156.             setCopperList( Hei, 1, VP, &custom );
  157.         case 2:
  158.             SetRGB4( VP, 1, 15, 15, 15 );
  159.             break;
  160.         }
  161.         
  162.         numc = getTime( scrClock, Prefs );
  163.         while(( len = TextLength( RP, scrClock, numc )) >= Wid )
  164.             numc--;
  165.         
  166.         x = ( Wid - len ) / 2;
  167.         y = ( Hei - fonty ) / 2;
  168.         
  169.         SetAPen( RP, 1 );
  170.         Move( RP, x, y + base );
  171.         Text( RP, scrClock, numc );
  172.         
  173.         Wnd = BlankMousePointer( Scr );
  174.         ScreenToFront( Scr );
  175.         
  176.         while( RetVal == OK )
  177.         {
  178.             WaitTOF();
  179.  
  180.             if(!( ScrToFrontCnt++ % ( 20 * Prefs[SECS].po_Active ? 1 : 6 )))
  181.             {
  182.                 numc = getTime( scrClock, Prefs );
  183.                 while(( nlen = TextLength( RP, scrClock, numc )) + x > Wid )
  184.                     numc--;
  185.                 if( Diff( oldClock, scrClock ))
  186.                 {
  187.                     Move( RP, x, y + base );
  188.                     Text( RP, scrClock, numc );
  189.                     
  190.                     if( nlen < len )
  191.                     {
  192.                         SetAPen( RP, 0 );
  193.                         RectFill( RP, x + nlen, y - 2, x + len + 2,
  194.                                  y + fonty + 2 );
  195.                         SetAPen( RP, 1 );
  196.                     }
  197.                     len = nlen;
  198.                     strcpy( oldClock, scrClock );
  199.                 }
  200.             }
  201.             
  202.             if(!( ScrToFrontCnt % 60 ))
  203.                 ScreenToFront( Scr );
  204.  
  205.             if( !delay || !( ++count % delay ))
  206.             {
  207.                 ScrollRaster( RP, dx, dy, x-2, y-2, x+len+2, y+fonty+2 );
  208.                 x -= dx;
  209.                 y -= dy;
  210.                 if( x < 3 )
  211.                     dx = -1;
  212.                 else
  213.                     if( x > Wid-len-3 )
  214.                         dx = 1;
  215.                 if( y < 3 )
  216.                     dy = -1;
  217.                 else
  218.                     if( y > Hei-fonty-3 )
  219.                         dy = 1;
  220.             }
  221.  
  222.             if( !Prefs[CYCLE].po_Active )
  223.             {
  224.                 if(!( count % 10 ))
  225.                 {
  226.                     c1 = ++c1 % 42;
  227.                     c2 = ++c2 % 42;
  228.                     c3 = ++c3 % 42;
  229.                     SetRGB4( VP, 1, vals[c1], vals[c2], vals[c3] );
  230.                 }
  231.             }
  232.  
  233.             RetVal = ContinueBlanking();
  234.         }
  235.         if( Prefs[CYCLE].po_Active == 3 )
  236.             clearCopperList( VP );
  237.         UnblankMousePointer( Wnd );
  238.         CloseScreen( Scr );
  239.     }
  240.     else
  241.         RetVal = FAILED;
  242.     
  243.     if( font )
  244.         CloseFont( font );
  245.     
  246.  JAIL:
  247.     if( UtilityBase )
  248.         CloseLibrary( UtilityBase );
  249.     if( DiskfontBase )
  250.         CloseLibrary( DiskfontBase );
  251.  
  252.     return RetVal;
  253. }
  254.